#if !defined(__APP_DEVICE_H__)
#define __APP_DEVICE_H__

#include "app_buffer.h"
#include "app_message.h"

struct vTable;

typedef struct
{
    struct vTable *vptr;            // 设备的虚表
    char *filename;                 // 设备名称
    int fd;                         // 设备文件描述符
    ConnectionType connection_type; // 设备连接类型
    Buffer recv_buffer;             // 从设备描述符中读取数据的缓存
    Buffer send_buffer;             // 消息路由向设备写入数据时的缓存
    int is_running;                 // 设备是否在运行
    pthread_t background_thread;    // 设备的后台线程
} Device;

struct vTable
{
    int (*recv_callback)(Device *, void *, int);
    void *(*background_func)(void *);
    int (*post_read)(Device *, void *, int);
    int (*pre_write)(Device *, void *, int);
    void (*recv_task)(void *);
    void (*send_task)(void *);
};

int app_device_init(Device *device, char *filename);

int app_device_start(Device *device);

void app_device_registerRecvCallback(Device *device, int (*callback)(Device *, void *, int));

int app_device_write(Device *device, void *ptr, int len);

int app_device_stop(Device *device);

void app_device_close(Device *device);

#endif // __APP_DEVICE_H__
